--[[
    使用例子
    local uid = 123456
    local test_orm = g.lormfactory:make_lorm_test()
    test_orm:load(uid)
    local test_data = test_orm:get()
    test_data.tax = 100
    test_data.total_tax = 100
    test_data.jdata = {a=1, b=2}
    test_orm:set_dirty(uid, "tax", "jdata")
    test_orm:save()

    local lormfactory = new("lormfactory")
    local test_orm  = lormfactory.make_lorm_test2()
    local uid = 1
    local rid = 1
    if not test_orm:load(uid) then
        test_orm = nil
        error("test_orm load uid failed!")
    end
    local _dt = test_orm:get(rid)
    if _dt == nil then
        _dt = {
            tax = 100,
            jdata = {a=1, b=2}
        }
        test_orm:update(rid, _dt)
    end
    test_orm:save()
    
--]]


local lorm = require "lorm"
local json = require "cjson"

local DT = lorm.DT

local M = class("LormFactory")

function M:ctor()

end

function M:make_lorm_test()
    local fields = {
        uid = {Datatype = DT.Integer,},
        rid = {Datatype = DT.Integer,},
        tax = {Datatype = DT.Integer, Default = 0},
        jdata = {Datatype = DT.String, Default = "[]",},
    }

   local function encode(r)
        g.log:fdebug("make_lorm_test encode r:%s", tostring(r))
        if r.jdata then
            r.jdata = json.encode(r.jdata)
        end
        return r
    end

    local function decode(r)
        if r.jdata then
            local obj, err = json.decode(r.jdata)
            if not obj then
                g.log:err("make_lorm_test decode failed! %s", err)
                return
            end
            r.jdata = obj
        end
        g.log:fdebug("make_lorm_test decode r:%s", tostring(r))
        return r
    end

    return lorm.new("lorm_test", fields, "uid", nil,
                    "update_single",
                    "load_single",
                    "delete_single",
                    encode, decode)
end


function M:make_lorm_test2()
    local fields = {
        uid = {Datatype = DT.Integer,},
        rid = {Datatype = DT.Integer,},
        tax = {Datatype = DT.Integer, Default = 0},
        jdata = {Datatype = DT.String, Default = "[]",},
    }
    local function encode(r)
        g.log:fdebug("make_lorm_test2 encode r:%s", tostring(r))
        if r.jdata then
            r.jdata = json.encode(r.jdata)
        end
        return r
    end
    local function decode(r)
        if r.jdata then
            local obj, err = json.decode(r.jdata)
            if not obj then
                g.log:err("make_lorm_test2 decode failed! %s", err)
                return
            end
            r.jdata = obj
        end
        g.log:fdebug("make_lorm_test2 decode r:%s", tostring(r))
        return r
    end
    return lorm.new("lorm_test", fields, "uid", "rid",
            "update_multi",
            "load_multi",
            "delete_multi",
            encode, decode)
end

function M:makeMatchRoom()
    local fields = {
        rid = {Datatype = DT.Integer,},
        start_time = {Datatype = DT.Integer, Default = 0},
        end_time = {Datatype = DT.Integer, Default = 0},
        enroll_status = {Datatype = DT.String, Default = "[]",},
        pool = {Datatype = DT.Integer, Default = 0},
        reward_pool = {Datatype = DT.Integer, Default = 0},
        blind_level = {Datatype = DT.Integer, Default = 0},
        game_status = {Datatype = DT.Integer, Default = 0},
    }
    return lorm.new("texas_match_room", fields, "rid", nil)

end

function M:makeMatchRoomUser()
    local fields = {
        mid = {Datatype = DT.Integer,},
        rid = {Datatype = DT.Integer,},
        tid = {Datatype = DT.Integer,},
        chip = {Datatype = DT.Integer, Default = 0},
        rank = {Datatype = DT.Integer, Default = 0},
        start_time = {Datatype = DT.Integer, Default = 0},
        end_time = {Datatype = DT.Integer, Default = 0},
        award = {Datatype = DT.Integer, Default = 0},

    }
    return lorm.new("texas_match_room_user", fields, "rid", "mid",
            "update_multi",
            "load_multi",
            "delete_multi")

end

function M:makeUserGameChip()
    local fields = {
        mid = {Datatype = DT.Integer,},
        info = {Datatype = DT.String, Default = "[]"},
    }
    local function encode(r)
        if r.info then
            r.info = json.encode(r.info)
        end
        return r
    end
    local function decode(r)
        if r.info then
            local obj, err = json.decode(r.info)
            if not obj then
                g.log:err("makeUserGameChip decode failed! %s", err)
                return
            end
            r.info = obj
        end
        return r
    end
    return lorm.new("member_game_chip", fields, "mid", "nil",
            "update_single",
            "load_single",
            "delete_single",
            encode, decode)

end

--金币
function M:makeUserGameInfo()
    local fields = {
        mid = {Datatype = DT.Integer,},
        info = {Datatype = DT.String, Default = "[]"},
    }
    local function encode(r)
        if r.info then
            r.info = json.encode(r.info)
        end
        return r
    end
    local function decode(r)
        if r.info then
            local obj, err = json.decode(r.info)
            if not obj then
                g.log:err("makeUserGameChip decode failed! %s", err)
                return
            end
            r.info = obj
        end
        return r
    end
    return lorm.new("member_game_info", fields, "mid", "nil",
            "update_single",
            "load_single",
            "delete_single",
            encode, decode)

end


return M
